package com.xique.door.device.hk.service;

import com.alibaba.fastjson.JSON;

import com.xique.door.constant.AccessServiceName;
import com.xique.door.util.RestTemplateUtils;
import com.xique.door.device.hk.bean.param.*;
import com.xique.door.device.hk.bean.response.BasicResponse;
import com.xique.door.device.hk.bean.response.PermissionGroupResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2021/7/13 11:41
 */
@Slf4j
@Service
public class HikAccessCommandService {

    @Value("${ruoyi.faceApiUrl_HK}")
    private String apiUrl;

    @Autowired
    private RestTemplateUtils restTemplateUtils;

    /**
     * 1.添加权限组
     * 2.删除权限组
     * 3.更新权限组
     * 4.权限组绑定设备
     * 5.权限组解绑设备
     * 6.权限组绑定用户
     * 7.权限组解绑用户
     */

    private Map<String, String> getHeader() {
        Map<String, String> map = new HashMap<>(2);
        map.put("Authorization", "bearer 432b6e20-15cc-482f-b2e5-53b6c399f5f4");
        map.put("Content-Type", "application/json");
        return map;
    }

    /**
     * 权限组添加
     * @author shd
     * @date 2021/7/13 13:42
     * @param param
     */
    public PermissionGroupResponse addPermissionGroup(HikPermissionGroupAddParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        PermissionGroupResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_GROUP_ADD, paramMap, getHeader(), PermissionGroupResponse.class);
        log.info("addPermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组更新
     * @author shd
     * @date 2021/7/13 13:43
     * @param param
     */
    public BasicResponse updatePermissionGroup(HikPermissionGroupUpdateParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_GROUP_UPDATE, paramMap, getHeader(), BasicResponse.class);
        log.info("updatePermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组删除
     * @author shd
     * @date 2021/7/13 13:42
     * @param param
     */
    public BasicResponse delPermissionGroup(HikPermissionGroupDelParam param) {
        String paramStr = "?groupId=" + param.getGroupId();
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_GROUP_DEL + paramStr, null, getHeader(), BasicResponse.class);
        log.info("deletePermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组绑定设备
     * @author shd
     * @date 2021/7/13 13:46
     * @param param
     */
    public BasicResponse addDevices(HikDeviceAddPermissionGroupParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_DEVICE_ADD, paramMap, getHeader(), BasicResponse.class);
        log.info("deviceAddPermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组解绑设备
     * @author shd
     * @date 2021/7/13 13:55
     * @param param
     */
    public BasicResponse delDevices(HikDeviceDelPermissionGroupParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_DEVICE_DEL, paramMap, getHeader(), BasicResponse.class);
        log.info("deviceDelPermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组绑定用户
     * @author shd
     * @date 2021/7/13 13:57
     * @param param
     */
    @Async
    public BasicResponse addPersons(HikPersonAddPermissionGroupParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_PERSON_ADD, paramMap, getHeader(), BasicResponse.class);
        log.info("personAddPermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }

    /**
     * 权限组解绑用户
     * @author shd
     * @date 2021/7/13 13:58
     * @param param
     */
    @Async
    public BasicResponse delPersons(HikPersonDelPermissionGroupParam param) {
        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(param), Map.class);
        BasicResponse openResult = restTemplateUtils.httpPostJson(apiUrl + AccessServiceName.OPEN_ACCESSCONTROL_PERSON_DEL, paramMap, getHeader(), BasicResponse.class);
        log.info("personDelPermissionGroup,param:{} openResult:{}", param, openResult);
        return openResult;
    }
}
